# -*- perl -*-

use Test::More tests => 11;
use HTML::FormFu;
use HTML::FormFu::DBIC ':all';
use lib 't/lib';
use DBSchema;
use YAML::Syck qw( Load );

my $dsn    = 'dbi:SQLite:dbname=t/var/dvdzbr.db';
my $schema = DBSchema->connect( $dsn, '', '', {} );
$schema->deploy({ add_drop_table => 1, });
$schema->populate('User', [
    [ qw/id username name password / ],
    [ 1, 'jgda', 'Jonas Alves', ''],
    [ 2, 'isa' , 'Isa', '', ],
    [ 3, 'zby' , 'Zbyszek Lukasiak', ''],
    ]
);
$schema->populate('Tag', [
    [ qw/id name file / ],
    [ 1, 'comedy', '' ],
    [ 2, 'dramat', '' ],
    [ 3, 'not_american', '' ],
    ]
);
$schema->populate('Dvd', [
    [ qw/id name imdb_id owner current_borrower creation_date alter_date / ],
    [ 1, 'Picnic at the Hanging Rock', 123, 1, 1, '', '' ],
    [ 2, 'The Deerhunter', 1234, 1, 1, '', '' ],
    [ 3, 'Rejs', 1235, 1, 1, '', '' ],
    [ 4, 'Seksmisja', 1236, 1, 1, '', '' ],
    ]
); 
$schema->populate( 'Dvdtag', [
    [ qw/ dvd tag / ],
    [ 3, 1 ],
    [ 4, 1 ],
    ]
);

my $form = HTML::FormFu->new( { stash => { schema => $schema } } );
$form->populate( Load( do { local $/; <DATA> } ) );
$form->process( {
        name => 'Rashomon',
        imdb_id => '1111',
        owner => '1',
        current_borrower => '2',
        tags => [ '2', '3' ],
    }
);
my $row = $schema->resultset( 'Dvd' )->new({});
    
populate_from_formfu( $row, $form );
$row = $schema->resultset( 'Dvd' )->find({ name => 'Rashomon' });
is ( $row->imdb_id, '1111', 'imdb_id set' );
is ( $row->owner->id, '1', 'owner set' );
is ( $row->current_borrower->id, '2', 'current_borrower set' );
ok ( ( grep { $_->name eq 'dramat' } $row->tags ), 'Tag "dramat" set' );
ok ( ( grep { $_->name eq 'not_american' } $row->tags ), 'Tag "not_american" set' );
is ( $row->tags->count, 2, 'two tags set' );

my $user_yaml = <<END;
---
elements:
  - type: Text
    name: username
  - type: Text
    name: name
  - type: Password
    name: password
  - type: Select::DBIC
    name: borrowed_dvds
    dbic_args: 
      resultset_name: Dvd
      search_attr: 
        order_by: 
          - id
END

$form = HTML::FormFu->new( { stash => { schema => $schema } } );
$form->populate( Load( $user_yaml ) );
$form->process( {
        username => 'zby',
        name => 'Zbigniew Lukasiak',
        borrowed_dvds => [ '3', '4' ],
        password => '',
    }
);
$row = $schema->resultset( 'User' )->find(2);
populate_from_formfu( $row, $form );
is ( $row->password, '', 'password set' );
is ( $row->name, 'Zbigniew Lukasiak', 'name set' );
ok ( ( grep { $_->id eq '3' } $row->borrowed_dvds), 'Dvd 3 borrowed set' );
ok ( ( grep { $_->id eq '4' } $row->borrowed_dvds), 'Dvd 4 borrowed set' );
is ( $row->borrowed_dvds->count, 2, 'two borrowed dvds' );


#warn Dumper( $by_pkey->_options ); use Data::Dumper;

__DATA__

---
elements:
  - type: Text
    name: name
  - type: Text
    name: imdb_id
  - type: Select::DBIC
    name: owner
    dbic_args: 
      resultset_name: User
      search_attr: 
        order_by: 
          - id
  - type: Text
    name: current_borrower
  - type: Select::DBIC
    name: tags
    multiple: 1
    dbic_args: 
      resultset_name: Tag
      search_attr: 
        order_by: 
          - id

